package com.famdoc.Service.Impl;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.famdoc.Mapper.PatientMapper;
import com.famdoc.Pojo.dto.CheckForm;
import com.famdoc.Pojo.dto.Result;
import com.famdoc.Pojo.entity.*;
import com.famdoc.Service.PatientService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PatientServiceImpl implements PatientService {
    @Resource
    private PatientMapper patientmapper;
    @Override
    public Result  addCheck(CheckForm checkForm){
        CheckItem checkItem = new CheckItem(checkForm);
        int checkItemId = patientmapper.addCheckItem(checkItem);
        if(checkItemId>0){
            CheckBody checkBody = new CheckBody(checkForm);
            checkBody.setCheckItemId(checkItem.getId());
            int checkId = patientmapper.addCheckBody(checkBody);
            if(checkId>0){
                    return Result.success();
            }
            return Result.error(201,"新增随访失败！");
        }
        return Result.error(201,"新增随访失败！");
    }


    public Result selectPatient(Integer page,Integer pageSize, Integer doctor_id,  String name,  String idNumber, String phoneNumber){
        PageHelper.startPage(page, pageSize);
        List<Patient> patientList=null;
        patientList = patientmapper.selectPatient(doctor_id,name,idNumber,phoneNumber);
        for (Patient patient: patientList){
           patient.setCheckForm(patientmapper.selectCheckItem(patient.getId()));
        }
        System.out.println(patientList);
        PageInfo pageInfo = new PageInfo(patientList);
        //封装数据
        Map<String, Object> patients = new HashMap<>();
        patients.put("checkItemList",pageInfo);
        //返回数据
        return Result.success(patients);
    }



    public List<CheckForm> selectCheckItem(Integer id){
        return patientmapper.selectCheckItem(id);
    }



    public Result updateCheckItem(CheckItem checkItem){
        Integer integer=patientmapper.updateCheckItem(checkItem);
        if(integer==1)
            return Result.success();
        else
            return Result.error(201,"修改失败，请重试！");
          }

    public Result count( String diseaseName,String startingTime,String endingTime){
        Map<String, Object> patients = new HashMap<>();
        startingTime=startingTime+" 00:00:00";
        endingTime=endingTime+" 23:59:59";
        System.out.println(startingTime);
        System.out.println(endingTime);
        patients.put("体检人数",patientmapper.countCheckItemPNum(diseaseName,startingTime,endingTime));
        patients.put("建档人数",patientmapper.countArchive(diseaseName,startingTime,endingTime));
        patients.put("签约人数",patientmapper.countSinging(diseaseName,startingTime,endingTime));
        patients.put("体检人次",patientmapper.countCheckItemPTim(diseaseName,startingTime,endingTime));
        patients.put("随访人次",patientmapper.countVisitPTim(diseaseName,startingTime,endingTime));
        patients.put("随访人数",patientmapper.countVisitPNum(diseaseName,startingTime,endingTime));
        return  Result.success(patients);
    }


    public  Result selectArchiveDetail (Integer page, Integer pageSize,String diseaseName){
        PageHelper.startPage(page, pageSize);
        List<Patient> patientList=null;
        patientList=patientmapper.selectArchiveDetail(diseaseName);
        PageInfo pageInfo = new PageInfo(patientList);
        //封装数据
        Map<String, Object> patients = new HashMap<>();
        patients.put("archiveList",pageInfo);
        //返回数据
        return Result.success(patients);
    }

    @Override
    public Result deleteCheck(Integer checkItem_id) {
        if(patientmapper.deleteCheck(checkItem_id)>0){
            return Result.success("体检记录删除成功！");
        }
        else{
            return Result.error(201,"体检记录删除失败！");
        }
    }

   @Override
   public  List<Patient> downloadPatientInfo(){
        return patientmapper.downloadPatientInfo();
   }

   @Override
   public Result uploadPatientInfo(List<Patient> list){
        patientmapper.uploadPatientInfo(list);
        return Result.success("患者档案上传成功！");
   }

   @Override
   public Integer UP(Patient patient){
        return  patientmapper.UP(patient);
   }

   @Override
   public List<Patient> list(InputStream inputStream) {
       List<Patient> list = new ArrayList<>();
       EasyExcel.read( inputStream ).head( Patient.class ).sheet() .headRowNumber(2)
               //通过监听器去解析表格
               .registerReadListener( new AnalysisEventListener<Patient>() {
                   @Override
                   //一行一行的进行解析
                   public void invoke(Patient patient, AnalysisContext analysisContext) {
                       list.add( patient );
                   }
                   @Override
                   //解析完成之后执行此回调
                   public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                       System.out.println( "解析完成" );
                   }
               } ).doRead();//doRead开始执行
       return list;
   }


}

